package com.hd.trans.files.sort;

import android.os.Build;

import androidx.annotation.RequiresApi;

import com.hd.trans.files.bean.MediaEntity;
import com.hd.trans.utils.FileUtils;
import com.hd.trans.utils.HanziToPinyin;

import java.text.Collator;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Author: yangtianwen
 * Time: 2021/12/27
 * Des: 媒体文件排序功能实现
 */
public class MediaSortImpl<T extends MediaEntity> implements ISort<T> {

    @Override
    public List<T> orderCreateTimeNewToOld(List<T> media) {
        Collections.sort(media, createTimeNewToOldComparator);
        return media;
    }

    @Override
    public List<T> orderCreateTimeOldToNew(List<T> media) {
        Collections.sort(media, createTimeOldToNewComparator);
        return media;
    }

    @Override
    public List<T> orderChangeTimeNewToOld(List<T> media) {
        Collections.sort(media, changeTimeNewToOldComparator);
        return media;
    }

    @Override
    public List<T> orderChangeTimeOldToNew(List<T> data) {
        Collections.sort(data, changeTimeOldToNewComparator);
        return data;
    }

    @Override
    public List<T> orderSizeSmallToLarge(List<T> data) {
        Collections.sort(data, sizeSmallToLarge);
        return data;
    }

    @Override
    public List<T> orderSizeLargeToSmall(List<T> data) {
        Collections.sort(data, sizeLargeToSmall);
        return data;
    }

    @Override
    public List<T> orderNameAToZ(List<T> data) {
        Collections.sort(data, nameAToZComparator);
        return data;
    }

    @Override
    public List<T> orderNameZToA(List<T> data) {
        Collections.sort(data, nameZToAComparator);
        return data;
    }

    private int longToCompareInt(long x, long y) {
        return Long.compare(x, y);
    }

    /**
     * 创建时间 新 - 旧
     */
    public Comparator<MediaEntity> createTimeNewToOldComparator = new MediaComparator() {
        @RequiresApi(api = Build.VERSION_CODES.O)
        @Override
        protected int doCompare(MediaEntity media1, MediaEntity media2) {
            long accessTime1 = FileUtils.getAccessTime(media1.getPath());
            long accessTime2 = FileUtils.getAccessTime(media2.getPath());
            return longToCompareInt(accessTime1, accessTime2);
        }
    };

    /**
     * 创建时间 旧 - 新
     */
    public Comparator<MediaEntity> createTimeOldToNewComparator = new MediaComparator() {
        @RequiresApi(api = Build.VERSION_CODES.O)
        @Override
        protected int doCompare(MediaEntity media1, MediaEntity media2) {
            long accessTime1 = FileUtils.getAccessTime(media1.getPath());
            long accessTime2 = FileUtils.getAccessTime(media2.getPath());
            return longToCompareInt(accessTime2, accessTime1);
        }
    };

    /**
     * 修改时间 新 - 旧
     */
    public Comparator<MediaEntity> changeTimeNewToOldComparator = new MediaComparator() {
        @Override
        protected int doCompare(MediaEntity media1, MediaEntity media2) {
            return longToCompareInt(media2.getDate(), media1.getDate());
        }
    };

    /**
     * 修改时间 旧 - 新
     */
    public Comparator<MediaEntity> changeTimeOldToNewComparator = new MediaComparator() {
        @Override
        protected int doCompare(MediaEntity media1, MediaEntity media2) {
            return longToCompareInt(media1.getDate(), media2.getDate());
        }
    };

    /**
     * 文件大小 小 - 大
     */
    public Comparator<MediaEntity> sizeSmallToLarge = new MediaComparator() {
        @Override
        protected int doCompare(MediaEntity media1, MediaEntity media2) {
            return longToCompareInt(media1.getSize(), media2.getSize());
        }
    };

    /**
     * 文件大小 大 - 小
     */
    public Comparator<MediaEntity> sizeLargeToSmall = new MediaComparator() {
        @Override
        protected int doCompare(MediaEntity media1, MediaEntity media2) {
            return longToCompareInt(media2.getSize(), media1.getSize());
        }
    };

    /**
     * 名称 A - Z
     */
    public Comparator<MediaEntity> nameAToZComparator = new MediaComparator() {

        @Override
        protected int doCompare(MediaEntity media1, MediaEntity media2) {
            String name1 = HanziToPinyin.getPinyinName(media1.getName());
            String name2 = HanziToPinyin.getPinyinName(media2.getName());
            return Collator.getInstance().compare(name1, name2);
        }
    };

    /**
     * 名称 Z - A
     */
    public Comparator<MediaEntity> nameZToAComparator = new MediaComparator() {

        @Override
        protected int doCompare(MediaEntity media1, MediaEntity media2) {
            String name1 = HanziToPinyin.getPinyinName(media2.getName());
            String name2 = HanziToPinyin.getPinyinName(media1.getName());
            return Collator.getInstance().compare(name1, name2);
        }
    };

    private abstract static class MediaComparator implements Comparator<MediaEntity> {

        @Override
        public int compare(MediaEntity media1, MediaEntity media2) {
            if (!media1.isDirectory() == !media2.isDirectory()) {
                return doCompare(media1, media2);
            }
            return media1.isDirectory() ? -1 : 1;
        }

        protected abstract int doCompare(MediaEntity media1, MediaEntity media2);
    }
}
